import graph_tool as gt
from old_gt import gt_graph_io
from old_gt import gt_graph_sim_indices as gsim
import numpy as np
from itertools import izip
import timeit
import graph_tool.spectral as gts

def get_degrees_dic(g):
    degs = {}
    for v in g.vertices():
        index = int(v)
        deg = v.out_degree()
        degs[index] = deg
    return degs
        

def test_creating_data_matrix():
    file_path = "/Users/rockyrock/Desktop/Netwoks/data/facebook/all.txt"
#     file_path = "./edges.txt"
    print "Loading the graph..."
    start = timeit.default_timer()
    graph = gt_graph_io.read_graph(file_path)
#     v1 = graph.g.vertex(731)
#     v2 = graph.g.vertex(725)
#     l1 = graph.get_v_label(v1)
#     l2 = graph.get_v_label(v2)
#     print l1, l2
    stop = timeit.default_timer()
    print "Done loading the graph in %d seconds." % (stop-start)
    g = graph.g
    print "Number of v = %d, Number of e = %d." % (g.num_vertices(), g.num_edges())
    a_start = timeit.default_timer()
    A = gts.adjacency(g)
    a_stop = timeit.default_timer()
    print "Getting A:", a_stop - a_start
    
    deg_start = timeit.default_timer()
    get_degrees_dic(g)
    deg_stop = timeit.default_timer()
    print "Degrees time:", deg_stop - deg_start
    
    print "Loading the data matrix..."
    similarity_indices = list()
    
    print "1- Computing CN..."
    cn_start = timeit.default_timer()
    S_cn = gsim.cn(g)
    cn_stop = timeit.default_timer()
    print "CN time:", cn_stop-cn_start
    
    print "2- Computing PA..."
    pa_start = timeit.default_timer()
    S_pa = gsim.pa(g)
    pa_stop = timeit.default_timer()
    print "PA time:", pa_stop-pa_start
    
    print "3- Computing X..."
    x_start = timeit.default_timer()
    S_x = gsim.jacard(g)
    x_stop = timeit.default_timer()
    print "X time:", x_stop - x_start
    
    similarity_indices.append(S_cn)
    similarity_indices.append(S_pa)
    similarity_indices.append(S_x)
    
    nrows = len(graph.target)
    ncols = len(similarity_indices)#number of features
    data = np.zeros((nrows,ncols))
    graph.data = data
    for i, edge_id in enumerate(graph.edges_ids):
        v1_label, v2_label = gt_graph_io.from_edgeID_to_vertix_labels(edge_id)
        v1_index = graph.get_v_index(v1_label)
        v2_index = graph.get_v_index(v2_label)
        for j, S in enumerate(similarity_indices):
            data[i, j] = S[v1_index, v2_index]
    
#     for edge_id, features, linked in izip(graph.edges_ids, data, graph.target):
#         print edge_id, ":", features, linked
    print data.shape
    print "Done loading the data matrix."

def test():
    test_creating_data_matrix()
    
test()












